package cn.ffcs.uec.staff.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.staff.action.bean.UecAssEditBean;
import cn.ffcs.uec.staff.model.UecAss;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;

@Controller
@Scope("prototype")
public class UecAssEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecAssEditBean bean = new UecAssEditBean();
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 人员信息
	 */
	private UecStaffInfo uecStaffInfo;
	/**
	 * 添加教育经历
	 */
	private UecAss uecAss;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化窗口
	 * @throws ParseException 
	 */
	public void onCreate$uecAssEditWindow() throws ParseException {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {
		//考核期间类型glbdef5
		List<NodeVo> nodeVoGlbdef5List = new ArrayList<NodeVo>();
		String[][] glbdef5 = {{"1001A11000000000061S", "年度"},
				{"1001A11000000000061T", "半年"},
				{"1001A11000000000061U", "季度"},
				{"1001A11000000000061V", "月度"},
				{"1001A11000000000061W", "聘任期"}, 
				{"1001A11000000000061X", "以项目为周期"},
				{"1001A11000000000061Y", "周"},
				{"1001A11000000000061Z", "见习期"},
				{"1001A110000000000620", "试用期"},
				{"1001A110000000000621", "合同期"}};
		for (int i = 0; i < glbdef5.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(glbdef5[i][j]);
				nodeVo.setName(glbdef5[i][j + 1]);
			}
			nodeVoGlbdef5List.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getGlbdef5(), nodeVoGlbdef5List);
		
	}

	/**
	 * bindBean
	 * @throws ParseException 
	 */
	public void bindBean() throws ParseException {
		opType = (String) arg.get("opType");
		if ("add".equals(opType)) {
			this.bean.getUecAssEditWindow().setTitle("新增人员考核信息");
			bean.getUecAssEditDiv().setHeight("30%");
			uecStaffInfo = (UecStaffInfo) arg.get("uecStaffInfo");
		}
		if ("view".equals(opType)) {
			this.bean.getUecAssEditWindow().setTitle("查看人员考核信息");
			uecAss = (UecAss) arg.get("uecAss");
			if (null != uecAss) {
				PubUtil.fillBeanFromPo(uecAss, bean);
				stringTransToDate();
				bean.getButtonDiv().setVisible(false);
				PubUtil.disableComponent(bean.getUecAssEditWindow(),true);
			}
		}
		if ("edit".equals(opType)) {
			this.bean.getUecAssEditWindow().setTitle("编辑人员考核信息");
			bean.getUecAssEditDiv().setHeight("30%");
			uecAss = (UecAss) arg.get("uecAss");
			if (null != uecAss) {
				PubUtil.fillBeanFromPo(uecAss, bean);
				stringTransToDate();
			}
		}
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		if ("add".equals(opType)) {
			uecAss = UecAss.newInstance();
			PubUtil.fillPoFromBean(bean, uecAss);
			dateTransToString();
			uecAss.setStaffId(uecStaffInfo.getStaffId());
			uecAss.addOnly();
			Events.postEvent("onOK", this.self, uecAss);
			this.onCancel();
			Messagebox.show("新增成功！");
		}
		if ("edit".equals(opType)) {
			PubUtil.fillPoFromBean(bean, uecAss);
			dateTransToString();
			uecAss.updateOnly();
			Events.postEvent("onOK", this.self, uecAss);
			this.onCancel();
			Messagebox.show("更新成功！");
		}
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecAssEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		// if (StrUtil.isNullOrEmpty(bean.getEncourName().getValue())) {
		// return "填写奖励名称";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourDate().getValue())) {
		// return "填写奖励日期";
		// }
		return null;
	}
	/**
	 * 将Date类型时间转成String类型
	 * Date --> String
	 * 一套模型，两套标准：日期类型需要格式化
	 */
	public void dateTransToString() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd");
		if (!StrUtil.isNullOrEmpty(bean.getGlbdef2().getValue())) {
			// 起始日期（干部任免）
			uecAss.setGlbdef2(formatter.format(bean.getGlbdef2().getValue()));
		}

	}
	/**
	 * 将String类型的时间转换成Date类型
	 * 
	 * @throws ParseException
	 */
	public void stringTransToDate() throws ParseException {
		// 时间类型转换：String --> Date
		// 起始日期（干部任免）
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
		if (null != uecAss.getGlbdef2()) {
			String glbdef2 = uecAss.getGlbdef2();
			if(glbdef2.length() == 7){
				glbdef2 = glbdef2 +".01";
			}
			if(glbdef2.length() == 4){
				glbdef2 = glbdef2 +".01.01";
			}
			bean.getGlbdef2()
					.setValue(sdf.parse(glbdef2));
		}
	}

}
